﻿using Max.PH5_2008A.ShoppingCode.IRespository;
using Max.PH5_2008A.ShoppingCode.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH5_2008A.ShoppingCode.Service
{
    public class BaseService<T> : IBaseService<T> where T : class, new()
    {
        IBaseRespository<T> _respositry;

        public BaseService(IBaseRespository<T> respositry)
        {
            _respositry = respositry;
        }

        public bool Delete(List<T> ts)
        {
            bool result = _respositry.Delete(ts);
            return result;

        }

        public bool Insert(T t)
        {
            bool result =  _respositry.Insert(t);
            return result;
        }

        public IQueryable<T> Query()
        {
            return _respositry.Query();
        }

        public IQueryable<T> Query(int pageIndex, int pageSize,out int count)
        {
            var data = _respositry.Query();
             count = data.Count();
            var list = data.Skip((pageIndex-1) * pageSize).Take(pageSize);
            return list;
        }


        public IQueryable<T> Query<TKey>(int pageIndex, int pageSize, Func<T,TKey> sortFunc, bool sortIsDesc, out int count)
        {
            var data = _respositry.Query();

            if (sortIsDesc)
                data = data.OrderByDescending(sortFunc).AsQueryable();
            else
                data = data.OrderBy(sortFunc).AsQueryable();

            count = data.Count();
            var list = data.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return list;
        }

        public IQueryable<T> Query(string where, int pageIndex, int pageSize, out int count)
        {
            /*
             * 2023/3/1 , 没有用查询条件
             * **/
            var data = _respositry.Query();
            count = data.Count();

            var result = data.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return result;

        }

        public T QueryFirstById(string id)
        {
            throw new NotImplementedException();
        }

        public bool Update(T t)
        {
            bool result = _respositry.Update(t);
            return result;
        }
    }
}
